import express from "express"
import cookieParser from "cookie-parser"
import path from 'node:path'
import { access, constants, mkdir } from 'node:fs/promises'
import upload from "./uploadRouter";

const baseRouter = express.Router();

//允许跨域
baseRouter.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "content-type");
  res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
  res.header("Access-Control-Allow-Credentials", "true");
  if (req.method.toLowerCase() == "options")
    res.sendStatus(200); //让options尝试请求快速结束
  else next();
});

// 设置静态文件夹
const staticPath = path.resolve(process.cwd(), "./static")
// 静态目录
baseRouter.use(express.static(staticPath));
// cookie解析
baseRouter.use(cookieParser());
// 解析json
baseRouter.use(express.json());
// 解析表单urlencoded
baseRouter.use(express.urlencoded({ extended: true }));

baseRouter.use(upload);

export default baseRouter;
